<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/21
 * Time: 9:57
 */

namespace app\admin\controller;


use think\Controller;

/**
 * Class Type
 * @package app\admin\controller
 * 类型控制器
 */
class Type extends Controller
{
    protected $db;
    public function __construct()
    {
        parent::__construct();
        $this->db = new \app\common\model\Type();
    }
    //类型主页
    public function index()
    {
        //分配数据
        $data=$this->db->select();
        $this->assign('data',$data);
        //视图
        return view();
    }
    //类型添加、编辑
    public function add()
    {
        $tid=input('param.tid');
        if($tid !== null){
            $oldData=db('type')->where('tid',$tid)->value('tname');
            if(request()->isPost()){
            $data=input('post.');
            $res=$this->db->add($data);
            if ($res['valid']) {
                $this->success($res['msg'], 'index');
            } else {
                $this->error($res['msg']);
            }
            }
        }else {
            $oldData='';
            if (request()->isPost()) {
                $data = input('post.');
                $res = $this->db->add($data);
                if ($res['valid']) {
                    $this->success($res['msg'], 'index');
                } else {
                    $this->error($res['msg']);
                }
            }
        }
        $this->assign('oldData',$oldData);
        //视图
        return view();
    }
    //删除
    public function del($tid){
        $res = $this->db->del($tid);
        if ($res['valid']){
            $this->success($res['msg']);
        }else{
            $this->error($res['msg']);
        }
    }
}